package com.supermap.gwfs.monitor.controller;

import com.alibaba.fastjson.JSONObject;
import com.supermap.gwfs.monitor.entity.ForecastProduct;
import com.supermap.gwfs.monitor.entity.GridForecast;
import com.supermap.gwfs.monitor.service.IProductMonitorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaolu on 2017/8/17.
 */
@RestController
@EnableAutoConfiguration
@RequestMapping("/monitor")
public class FileMonitorController {
    private static Logger logger = LoggerFactory.getLogger(FileMonitorController.class);
    private static final String PARAM_VALID_ERROR = "PARAM_VALID_ERROR";

    @Resource
    IProductMonitorService productMonitorService;

    @RequestMapping("/gridMonitoring")
    @ResponseBody
    public JSONObject gridForecastMonitoring( HttpServletRequest request) {
        JSONObject result = new JSONObject();
        String date = request.getParameter("date");
        String sequence = request.getParameter("sequence");
        String version = request.getParameter("version");
        String origin = request.getParameter("originId");
        logger.debug("date:" + date + "<===>sequence:" + sequence + "<===>version:" + version + "<===>originId:" + origin);
        if (date == null || sequence == null
                || origin == null || version == null) {
            result.put("status", 0);
            result.put("message", PARAM_VALID_ERROR);
            return result;
        }
        Map param = new HashMap<String,String>();
        param.put("date",date);
        param.put("sequence",sequence);
        param.put("versions",version.split(";"));
        //param.put("element",element);
        param.put("origin",origin);
        List<GridForecast> gridForecastData =  productMonitorService.getGridForecastData(param);
        Map<String,List<GridForecast>> resultMap = new HashMap<String, List<GridForecast>>();
        for (GridForecast forecastProduct:gridForecastData) {
            if(!resultMap.containsKey(forecastProduct.getProductElement())){
                List<GridForecast> list = new ArrayList<GridForecast>();
                resultMap.put(forecastProduct.getProductElement(),list);
            }
            resultMap.get(forecastProduct.getProductElement()).add(forecastProduct);
        }
        List<JSONObject> dataList = new ArrayList<JSONObject>();
        for (int i = 0; i < resultMap.keySet().size(); i++) {
            String key = (String) resultMap.keySet().toArray()[i];
            JSONObject data = new JSONObject();
            GridForecast tmp = resultMap.get(key).get(0);
            data.put("id",i + 1 );
            data.put("productElement",tmp.getProductElement());
            data.put("productValue",tmp.getProductValue());
            data.put("productDate",tmp.getProductDate());
            data.put("productSequence",tmp.getProductSequence());
            List<String> validValueList = new ArrayList<String>();
            for (GridForecast forecastProduct: resultMap.get(key)) {
                if(!validValueList.contains(forecastProduct.getValidValue())){
                    validValueList.add(forecastProduct.getValidValue());
                }
                //productValidValue.append(forecastProduct.getValidValue()).append(", ");
            }
            data.put("productFileName",getValidValue(validValueList));
            dataList.add(data);
        }
        result.put("status", 1);
        result.put("data", dataList);
        return result;
    }

    private String getValidValue(List<String> validValueList) {
        String value = "";
        for (String validValue:validValueList) {
            value = value + validValue + " ,";
        }
        return value.substring(0,value.lastIndexOf(","));
    }

    @RequestMapping("/productMonitoringGet")
    @ResponseBody
    public JSONObject productMonitoringGet(HttpServletRequest request) {
        String date = request.getParameter("date");
        String sequence = request.getParameter("sequence");
        String version = request.getParameter("version");

        JSONObject result = new JSONObject();
        if (date == null || sequence == null || version == null) {
            result.put("status", 0);
            result.put("message", PARAM_VALID_ERROR);
            return result;
        }
        Map param = new HashMap<String,String>();
        param.put("date",date);
        param.put("sequence",sequence);
        param.put("version",version);
        List<ForecastProduct> forecastProductData =  productMonitorService.getForecastProductData(param);
        Map<String,List<ForecastProduct>> resultMap = new HashMap<String, List<ForecastProduct>>();
        for (ForecastProduct forecastProduct:forecastProductData) {
            if(!resultMap.containsKey(forecastProduct.getProductElement())){
                List<ForecastProduct> list = new ArrayList<ForecastProduct>();
                resultMap.put(forecastProduct.getProductElement(),list);
            }
            resultMap.get(forecastProduct.getProductElement()).add(forecastProduct);
        }
        List<JSONObject> dataList = new ArrayList<JSONObject>();
        for (int i = 0; i < resultMap.keySet().size(); i++) {
            String key = (String) resultMap.keySet().toArray()[i];
            JSONObject data = new JSONObject();
            ForecastProduct tmp = resultMap.get(key).get(0);
            data.put("id",i + 1 );
            data.put("productElement",tmp.getProductElement());
            data.put("productValue",tmp.getProductValue());
            data.put("productDate",tmp.getProductDate());
            data.put("productSequence",tmp.getProductSequence());
            data.put("productVersion",tmp.getProductVersion());
            StringBuilder productFileName = new StringBuilder();
            StringBuilder productCreate = new StringBuilder();
            for (ForecastProduct forecastProduct: resultMap.get(key)) {
                productFileName.append(forecastProduct.getProductFileName()).append("<br>");
                productCreate.append(forecastProduct.getProductCreate().substring(0,forecastProduct.getProductCreate().lastIndexOf("."))).append("<br>");
            }
            data.put("productFileName",productFileName.toString());
            data.put("productCreate",productCreate.toString());
            dataList.add(data);
        }
        result.put("status", 1);
        result.put("data", dataList);
        return result;
    }
}
